package cn.gd.controller.LeaveApplicationController;


import cn.gd.pojo.LeaveAppLiCations;
import cn.gd.service.LeaveApplicationService.LeaveApplicationService;
import cn.gd.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/leave - applications")
public class LeaveApplicationController {

    @Autowired
    private LeaveApplicationService leaveApplicationService;


    // 获取审批人
    @GetMapping("/getApprover/{applicationId}")
    public Result<List<LeaveAppLiCations>> getApprover(@PathVariable Integer applicationId) {
        List<LeaveAppLiCations> approver = leaveApplicationService.getApprover(applicationId);
        return Result.ok(approver);
    }

    // 获取所有请假申请
    @GetMapping("/findAll")
    public Result<List<LeaveAppLiCations>> findAll() {
        List<LeaveAppLiCations> list = leaveApplicationService.findAll();
        return Result.ok(list);
    }

    // 根据ID获取请假申请
    @GetMapping("/findById/{applicationId}")
    public Result<LeaveAppLiCations> findById(@PathVariable Integer applicationId) {
        LeaveAppLiCations application = leaveApplicationService.findById(applicationId);
        if (application!= null) {
            return Result.ok(application);
        }
        return Result.error();
    }

    // 新增请假申请
    @PostMapping("/save")
    public Result<LeaveAppLiCations> save(@RequestBody LeaveAppLiCations leaveApplication) {
        LeaveAppLiCations saved = leaveApplicationService.save(leaveApplication);
        return Result.ok(saved);
    }

    // 更新请假申请
//    @Autowired
    @PutMapping("/update/{applicationId}")
    public Result<LeaveAppLiCations> update(@PathVariable Integer applicationId, @RequestBody LeaveAppLiCations leaveApplication) {
        leaveApplication.setLeaveID(applicationId);
        LeaveAppLiCations updated = leaveApplicationService.update(leaveApplication);
        if (updated!= null) {
            return Result.ok(updated);
        }
        return Result.error();
    }

    // 删除请假申请
    @DeleteMapping("/deleteById/{applicationId}")
    public ResponseEntity<Void> deleteById(@PathVariable Integer applicationId) {
        leaveApplicationService.deleteById(applicationId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
